package cn.yhjz.platform.system.config;

import cn.yhjz.platform.system.constant.Constant;
import cn.yhjz.platform.system.entity.SysUser;
import cn.yhjz.platform.system.util.LoginUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 拦截所有请求，把登录用户的信息放到线程变量中
 */
@Deprecated
@Slf4j
public class UserInfoInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        String userInfoJsonStr = (String) session.getAttribute(Constant.CURRENT_USER_SESSION_KEY);
        if (!StrUtil.isBlank((userInfoJsonStr))) {
            SysUser sysUser = JSON.parseObject(userInfoJsonStr, SysUser.class);
            LoginUtil.setCurrentUser(sysUser);
        }
        return true;
    }
}
